using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class CloneSkillController : Skill
{
    [Header("Clone info")]
    [SerializeField] private float attackMultiplier;
    [SerializeField] private GameObject prefab;
    private CloneSkill cloneSkill;
    [SerializeField] private float cloneDuration;
    [Space]
    [Header("Clone attack")]
    [SerializeField] private UiSkillTreeSlot cloneAttackUnlockButton;
    [SerializeField] private float cloneAttackMultiplier;
    [SerializeField] private bool canAttack;
    [Header("Aggressive clone")]
    [SerializeField] private UiSkillTreeSlot aggressiveCloneUnlockButton;
    [SerializeField] private float aggressiveCloneAttackMultiplier;
    public bool CanApplyOnHitEffect { get; private set; }
    [SerializeField] private bool createCloneOnCounterAttack;
    [Space]
    [Header("Multiple clone")]
    [SerializeField] private UiSkillTreeSlot multipleUnlockButton;
    [SerializeField] private float multiCloneAttackMultiplier;
    [SerializeField] private bool canDuplicateClone;
    // 35% chance to duplicate
    [SerializeField] private float chanceToDuplicate = 35f;
    [Header("Crystal instead of clone")]
    [SerializeField] private UiSkillTreeSlot crystalInsteadUnlockButton;
    public bool crystalInsteadOfClone;

    protected override void Start()
    {
        base.Start();
        cloneAttackUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockCloneAttack);
        aggressiveCloneUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockAggressiveClone);
        multipleUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockMultiClone);
        crystalInsteadUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockCrystalInstead);
    }

    protected override void CheckUnlock()
    {
        UnlockCloneAttack();
        UnlockAggressiveClone();
        UnlockMultiClone();
        UnlockCrystalInstead();
    }

    private void UnlockCloneAttack()
    {
        if (!cloneAttackUnlockButton.unlocked || canAttack) return;
        canAttack = true;
        attackMultiplier = cloneAttackMultiplier;
    }

    private void UnlockAggressiveClone()
    {
        if (!aggressiveCloneUnlockButton.unlocked || CanApplyOnHitEffect) return;
        CanApplyOnHitEffect = true;
        attackMultiplier = aggressiveCloneAttackMultiplier;
    }

    private void UnlockMultiClone()
    {
        if (!multipleUnlockButton.unlocked || canDuplicateClone) return;
        canDuplicateClone = true;
        attackMultiplier = multiCloneAttackMultiplier;
    }

    private void UnlockCrystalInstead()
    {
        if (!crystalInsteadUnlockButton.unlocked || crystalInsteadOfClone) return;
        crystalInsteadOfClone = true;
    }

    public void Create(Transform cloneTransform, Vector3 offset = default)
    {
        if (crystalInsteadOfClone)
        {
            SkillManager.Crystal.Create();
            return;
        }
        GameObject clone = Instantiate(prefab);
        clone.GetComponent<CloneSkill>().Setup(cloneTransform, cloneDuration, canAttack, offset,
            FindClosestEnemy(clone.transform),
            canDuplicateClone,
            chanceToDuplicate, Player, attackMultiplier);
    }

    public void CreateCloneWithDelay(Transform enemyTransform)
    {
        _ = StartCoroutine(CloneDelay(enemyTransform, new Vector3(1f * Player.FacingDirection, 0)));
    }

    private IEnumerator CloneDelay(Transform enemyTransform, Vector3 offset)
    {
        yield return new WaitForSeconds(0.4f);
        Create(enemyTransform, offset);
    }
}